package jym.tan.tmdb.json;

import org.apache.commons.lang.StringUtils;

import com.google.gson.Gson;

/**
 * The Class Parser. This the class which helps to parse the 
 * Json string into Java object. This is a Singleton class. 
 * To get an instance of this class invoke {@link #getInstance()} method
 * 
 * @author Tanmoy
 */
public class Parser {

	private static final Parser PARSER = new Parser();
	
	private final Gson gson = new Gson();
	
	/**
	 * Default Private Constructor.
	 */
	private Parser() {
		
	}
	
	/**
	 * Parses the Json String and generate Java object.
	 *
	 * @param json 
	 * 		The Json String
	 * @param clazz 
	 * 		The Class of the object to be converted into
	 * @return the t
	 * @throws 
	 * 		NullPointerException
	 */
	public <T> T parseJson(String json, Class<T> clazz) throws NullPointerException {
		if(StringUtils.isBlank(json)){
			throw new NullPointerException("Json String is Null");
		}
		return gson.fromJson(json, clazz);		
	}
	
	/**
	 * Gets the single instance of Parser.
	 *
	 * @return single instance of Parser
	 */
	public static final Parser getInstance() {
		return PARSER;
	}
}
